package edt;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;

import javax.swing.AbstractButton;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;

import org.joda.time.LocalTime;

public class EditLisener implements ActionListener {

	EditDialog v;
	MySchedulerModel model;
	CreneauModele creneauEdit;
	
	public EditLisener(EditDialog editDialog, MySchedulerModel model, CreneauModele creneauEdit) {
		this.v=editDialog;
		this.model=model;
		this.creneauEdit=creneauEdit;
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		
		
		model.removeAppointment(creneauEdit);
		model.addEventsToDelete(creneauEdit);
		
		
		String type = "";
		/* recuperation du type de cours */
	    for (Enumeration<AbstractButton> ee = v.bg.getElements(); ee.hasMoreElements();) {
	        JRadioButton b = (JRadioButton) ee.nextElement();
	        if (b.getModel() == v.bg.getSelection()) {
	        	type = b.getText();
	        }
	    }
		
	    
		LocalTime debut = new LocalTime(0,0,0);
		try {
			debut = new LocalTime(Integer.parseInt(v.debut.getValue().toString()),0,0);

		
			CreneauModele appointment = new CreneauModele(v.matiere.getText(), debut);
			
			appointment.setType(type);
			
			appointment.setDuration(Integer.parseInt(v.duree.getValue().toString()));
	
			
			appointment.setGroupe(v.groupe.getText());
			
			appointment.setClasse(v.classe.getText());
			
			
			if( v.date.getSelectedItem().equals("Lundi")) {
				appointment.set_resource(model.getLundi());
			}
			else if(v.date.getSelectedItem().equals("Mardi")) {
				appointment.set_resource(model.getMardi());
			}
			else if(v.date.getSelectedItem().equals("Mercredi")) {
				appointment.set_resource(model.getMercredi());
			}
			else if(v.date.getSelectedItem().equals("Jeudi")) {
				appointment.set_resource(model.getJeudi());
			}
			else if(v.date.getSelectedItem().equals("Vendredi")) {
				appointment.set_resource(model.getVendredi());
			}

			
			model.addAppointment(appointment);
		
			creneauEdit=appointment;
			
		
		} catch (NumberFormatException e1) {
			JOptionPane.showMessageDialog(v, "Erreur de saisie", "Erreur",
			        JOptionPane.ERROR_MESSAGE);		
			model.addAppointment(creneauEdit);
	    }
		
		
	}

}
